#ifndef _MESH_H
#define _MESH_H

#include "3DS.h"
#include "BoundingBox.h"

///* Geometric structure of the mesh. */
///* Triangle consists of {pointA, pointB, pointC} */
//class Triangle{
//public:
//	Vector3 pointA, pointB, pointC;
//public:
//	bool hasEdge(Vector3& edgeA, Vector3& edgeB){
//
//	}
//};


/* Mesh defines a renderable geometric structure. */
class Mesh{
public:
	Mesh();
	~Mesh();

	/* Load 3ds model to this mesh */
	void loadC3DSModel(C3DSModel* a3DSModel);

	/* get axial bounding box of the  mesh */
	BoundingBox& getBoundingBox();

	void initDepthPixels();	// Init front-back depth map. Must invoke this method before point test
	void initNormalPixels();	// Init front-back normal map.

	bool containsPoint(Vector3& apoint);	// Decide whether a point is inside the mesh
	Vector3 getOffsetVector(Vector3& apoint);
	Vector3 getNormal(Vector3& apoint);	// normal of surface point that is closest to the point
	Vector3 nearestSurfacePoint(Vector3& apoint);	// Coordinates of nearest surface point to a point that is inside/outside(not implemented) the mesh
	void scale(Vector3 scaleParam);	// scale around middle of the bounding box

	void renderFirstObject();

	void render();	// Render to graphic hardware
	void renderBoundingBox();
	void renderScaledMesh(Vector3 scale);
	void renderNormalMap();	// Render normal mesh (vertex colored as normals)

	/* NOTE: set position DOES NOT change internal data of 3d model. */
	void setPosition(Vector3& aPosition){
		position = aPosition;
		// recalculate boundingbox;
		calculateBoundingBox();
	}
	Vector3& getPosition(){return position;}

	t3DModel& getModel(){return model;}

private:
	void calculateBoundingBox();	// Calculate bounding box
public:
	float *frontDepthPixels, *backDepthPixels;
	float *frontNormalRPixels, *frontNormalGPixels, *frontNormalBPixels, *backNormalRPixels, *backNormalGPixels, *backNormalBPixels;

	int depthMapWidth, depthMapHeight;	// front-back depth map
	BoundingBox boundingBox;
private:
	
	t3DModel model;

	Vector3 position;	// Coordinates according to the world

	//bool isInitialized;
};

#endif